<?php

defined('BY_JYA') or exit('error');
/*
	游戏营销
*/
$tabs=array(
	array('op'=>'list','title'=>'盲盒列表'),
	array('op'=>'edit','title'=>'添加/编辑盲盒'),
);
set_html('tabs',$tabs);
$_RQ['op']=$_RQ['op']?:'list';
$return['types']=$this->canyu_types;
$return['rewardtype']=$this->rewardtype;
$return['get_types']=$this->get_types;
$return['times']=$this->times;
$return['addtimes_types']=$this->addtimes_types;
$return['prizeType']=$this->prizeType;
$return['he_opentype']=$this->he_opentype;
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'plugin_name'=>'jy_manghe','merchid'=>0);	
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if($_RQ['title']){
		$params['title like']=trim($_RQ['title']);
		$return['params']['title']=$_RQ['title'];
	}
	$parameter=array(
		'params'=>$params,
		'limit'=>array(intval($_RQ['page'])?:1,10),
	);
	$orderby=array();
	$pindex = max(1, intval($_RQ['page']));
	if($_RQ['num']){
		$psize = $_RQ['num'];
	}else{
		$psize = 10;
	}
	$total='';
	$list=pdo_getslice('jy_weishop_game',$params,array($pindex,$psize),$total,$fields,'','id desc');
	foreach($list as &$item){
		$item['pluginset']=json_decode($item['pluginset'],true);
		$item['sorturl']='jy_manghe/game//id='.$item['id'];
		$item['url']=custom_url($item['sorturl'],true);
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','tip'=>'[盲盒短链接]:可以用于小程序，和菜单设置链接；<red>盲盒的权重越大，说明奖品价值越高！方便机器人计算开奖！</red>','list'=>array(
			array('name'=>'title','title'=>'盲盒标题','type'=>'string'),
			array('name'=>'pluginset.he_opentype','title'=>'开奖算法','type'=>'radio','options'=>'he_opentype'),
			array('type'=>'unitstring','unit'=>'个','title'=>'倍率','name'=>'pluginset.he_onejifen'),
			array('name'=>'pluginset.he_maxjifen','title'=>'最大投入积分','type'=>'unitstring','unit'=>'个'),
			array('name'=>'pluginset.he_i','title'=>'几分钟开奖','type'=>'unitstring','unit'=>'分钟'),
			array('name'=>'status','title'=>'盲盒状态','type'=>'switch','set'=>'set'),
			array('name'=>'url','title'=>'盲盒长链接','type'=>'copy'),
			array('name'=>'sorturl','title'=>'盲盒短链接','type'=>'copy'),
			array('name'=>'url','title'=>'盲盒码','type'=>'ewm'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
		)),
		'search'=>array(
			array('type'=>'radio','width'=>15,'name'=>'status','title'=>'盲盒状态','options'=>'statuss'),
			array('type'=>'string','width'=>15,'name'=>'title','title'=>'盲盒标题'),
		),
	);	
	$return['htmls']=$htmls;
	$return['statuss']=array(
		'0'=>'关闭',
		'1'=>'启用',
	);
	exi($return);
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	$post=get_postdata();
	$status=intval($post['status']);
	pdo_update('jy_weishop_game',array('status'=>$status),array('id'=>$id));
	if($status==0){
		exi('关闭成功！');
	}else{
		exi('开启成功！');
	}
}
if($_RQ['op']=='edit'){
	if($_RQ['postdata']){
		$post=get_postdata();
		if(!$post['pluginset']['he_i'] || $post['pluginset']['he_i']<=2){
			exi('开奖时间不能小于2分钟！','error');
		}
        $post['pluginset']['he_ontime']='';
        $post['pluginset']['he_img']= $post['he_img'];
		$data=array(
			'title'=>trim($post['title']),
			'desc'=>trim($post['desc']),
			'game_id'=>intval($post['game_id']),
			'type'=>intval($post['type']),
			'price'=>round($post['price'],2),
			'goodsid'=>intval($post['goodsid']),
			'time'=>intval($post['time']),
			'status'=>intval($post['status']),
			'sendtitle'=>trim($post['sendtitle']),
			'sendimage'=>trim($post['sendimage']),
			'senddesc'=>trim($post['senddesc']),
			'pluginset'=>ijson_encode($post['pluginset']),
			'addtimes'=>ijson_encode($post['addtimes']),
			'reward'=>ijson_encode($post['reward']),
			'delredfee'=>trim($post['delredfee']),
			'delredtimes'=>trim($post['delredtimes']),
		);
		$id=intval($post['id']);
		if($id){
			pdo_update('jy_weishop_game',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$data['createtime']=TIMESTAMP;
			$data['action']='jy_manghe/game/';
			$data['plugin_name']='jy_manghe';
			pdo_insert('jy_weishop_game',$data);
		}
		exi('操作成功！','','list');
	}
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_weishop_game',array('id'=>$id));
		$item['pluginset']=$item['pluginset']?json_decode($item['pluginset'],true):array('rewardtype'=>0);
		$item['red_shareset']=$item['pluginset']['red_shareset']?:array();
	}else{
		$item=array('title'=>'','content'=>array('goodsid'=>''));
		$item['pluginset']=array('rewardtype'=>0);
		$item['red_shareset']=array();
	}
	$item['reward']=$item['reward']?json_decode($item['reward'],true):array();
	$item['sendtitle']=$item['sendtitle']?$item['sendtitle']:'[nickname]:恭喜您，获得一个参与奖励盲盒机会';
	$item['sendimage']=$item['sendimage']?$item['sendimage']:'../addons/jy_manghe/resource/images/icon.png';
    $item['pluginset']['he_img']=$item['pluginset']['he_img']?$item['pluginset']['he_img']:'../addons/jy_manghe/resource/images/icon.png';
	$item['senddesc']=$item['senddesc']?$item['senddesc']:'盲盒有时间限制，请及时参与！';
    $item['he_ontime']=$item['pluginset']['he_ontime'];
    $item['he_img']=$item['pluginset']['he_img'];
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基础设置',
				'list'=>array(
					array('name'=>'title','title'=>'盲盒标题','type'=>'string'),
					array('name'=>'pluginset.total','title'=>'盲盒总人次限制','type'=>'string'),
					array('name'=>'desc','title'=>'盲盒介绍','type'=>'text'),
					array('type'=>'string','name'=>'sendtitle','title'=>'推送标题','help'=>'变量 [nickname] 会员昵称'),
					array('type'=>'image','name'=>'sendimage','title'=>'活动封面'),
					array('type'=>'string','name'=>'senddesc','title'=>'推送说明','help'=>'变量 [nickname] 会员昵称'),	
					array('name'=>'status','title'=>'是否开启','type'=>'switch'),
			)),
			array(
				'title'=>'开奖设置',
				'list'=>array(
					//array('name'=>'goodsid','title'=>'选择商品','type'=>'choose','help'=>'','action'=>'jy_weishop/goods.get_goods/','show'=>array('type'=>2)),
					array('name'=>'pluginset.he_opentype','title'=>'开奖算法','type'=>'radio','options'=>'he_opentype','help'=>'最大奖励开奖：客户中奖率最大化（哪个盒子压的多，中奖价值越高）;[占总积分N%开出最高奖]:举例设置为20%开出为金豆盲盒，哪个盲盒最接近20%值即开出金豆盲盒；'),
                    array('type'=>'unitstring','unit'=>'%','title'=>'占总积分N%开出最高奖','name'=>'pluginset.he_maxopen','help'=>''),
                    array('type'=>'unitstring','unit'=>'个','title'=>'最小投入积分','name'=>'pluginset.he_minjifen','help'=>'最小投入积分,不能小于下面的倍率个数！'),
					array('type'=>'unitstring','unit'=>'个','title'=>'N个积分为倍率(盲盒票)','name'=>'pluginset.he_onejifen','help'=>'例如：100个积分为倍率，就只能投入100的倍数个积分，倍率*盒子奖励=开奖收益'),
					array('name'=>'pluginset.he_maxjifen','title'=>'每个盲盒最大投入积分数量','type'=>'unitstring','unit'=>'个','help'=>''),
					array('name'=>'pluginset.he_i','title'=>'每组拆盲盒时间为几分钟开奖一次','type'=>'unitstring','unit'=>'分钟','help'=>''),
					array('name'=>'pluginset.he_m','title'=>'每组拆盲盒倒数几秒钟内不能参与拆盲盒','type'=>'unitstring','unit'=>'秒钟','help'=>''),
					array('name'=>'pluginset.he_onenum','title'=>'同时一次能选择拆几个盲盒','type'=>'unitstring','unit'=>'个','help'=>''),
					array('name'=>'pluginset.he_onetime','title'=>'一组能参与拆几次','type'=>'unitstring','unit'=>'次','help'=>''),
                    array('name'=>'pluginset.opentime','title'=>'活动开始时间','type'=>'time','help'=>''),
                    //array('name'=>'he_ontime','title'=>'每天几点开启？','type'=>'time_hournew','help'=>''),
			)),
			array(
				'title'=>'盲盒设置',
				'list'=>array(
					array('type'=>'tip','title'=>'[<red>温馨提示</red>]：<red>权重越大，说明奖品价值越高！方便机器人计算开奖！</red>盲盒是随机组合的，没有顺序，没有概率。都是一次开奖，大家投积分押注，开奖的时候计算，具体怎么开奖！'),
                    array('type'=>'image','name'=>'he_img','title'=>'盲盒图片'),
                    array(
					'type'=>'array',
					'name'=>'reward',
                        'title'=>'盲盒设置',
					'add_tip'=>'添加一个盲盒',
						'list'=>array(
							array('type'=>'string','title'=>'盲盒名称','name'=>'title'),
							array('type'=>'string','title'=>'盲盒权重','name'=>'level'),
							array('type'=>'radio','title'=>'奖励类型','name'=>'type','help'=>'','options'=>'prizeType'),
							array('type'=>'string','title'=>'奖励数量','name'=>'jifen','show'=>array('type'=>array(2,3,4,5,6))),
						),
							'details'=>array(
								array('type'=>'string','title'=>'盲盒名称','name'=>'title'),
								array('type'=>'string','title'=>'盲盒权重','name'=>'level','help'=>'权重越大，说明奖品价值越高！方便机器人计算开奖！'),
								array('type'=>'radio','title'=>'奖励类型','name'=>'type','help'=>'','options'=>'prizeType'),
								array('type'=>'string','title'=>'奖励数量','name'=>'jifen'),
							)
					),
			)),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_weishop_game',array('id'=>$id));	
	}
	if(!$id){
		exi('请选择盲盒删除！','error');
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_weishop_game',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
